package com.jsh.erp.service.WorkflowConfigDetail;

import com.alibaba.fastjson.JSONObject;
import com.jsh.erp.constants.BusinessConstants;
import com.jsh.erp.datasource.entities.WorkflowConfigDetail;
import com.jsh.erp.datasource.entities.WorkflowConfigDetailExample;
import com.jsh.erp.datasource.mappers.WorkflowConfigDetailMapper;
import com.jsh.erp.datasource.mappers.WorkflowConfigDetailMapperEx;
import com.jsh.erp.exception.JshException;
import com.jsh.erp.service.log.LogService;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.utils.IdUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class WorkflowConfigDetailService {
    private Logger logger = LoggerFactory.getLogger(WorkflowConfigDetailService.class);

    @Resource
    private WorkflowConfigDetailMapper workflowConfigDetailMapper;
    @Resource
    private WorkflowConfigDetailMapperEx workflowConfigDetailMapperEx;
    @Resource
    private LogService logService;
    @Resource
    private SystemConfigService systemConfigService;

    public WorkflowConfigDetail getWorkflowConfigDetail(String id) throws Exception {
        return workflowConfigDetailMapper.selectByPrimaryKey(id);
    }

    public List<WorkflowConfigDetail> select(String mainId, String typeCode) throws Exception {
        List<WorkflowConfigDetail> list = null;
        try {
            list = workflowConfigDetailMapperEx.selectByMainId(mainId, typeCode);
        } catch (Exception e) {
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }

        return list;
    }

    public Long countWorkflowConfigDetail(String mainId, String typeCode) throws Exception {
        Long result = null;
        try {
            result = workflowConfigDetailMapperEx.countsByMainId(mainId, typeCode);
        } catch (Exception e) {
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int insertWorkflowConfigDetail(JSONObject obj, HttpServletRequest request) throws Exception {
        WorkflowConfigDetail workflowConfigDetail = JSONObject.parseObject(obj.toJSONString(), WorkflowConfigDetail.class);
        workflowConfigDetail.setUploadFlag("0");
        int result = 0;
        try {
            result = workflowConfigDetailMapper.insertSelective(workflowConfigDetail);
            logService.insertLog("分配权限",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(workflowConfigDetail.getActivityName()).toString(), request);
        } catch (Exception e) {
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int updateWorkflowConfigDetail(JSONObject obj, HttpServletRequest request) throws Exception {
        WorkflowConfigDetail workflowConfigDetail = JSONObject.parseObject(obj.toJSONString(), WorkflowConfigDetail.class);
        workflowConfigDetail.setUploadFlag("0");
        int result = 0;
        try {
            result = workflowConfigDetailMapper.updateByPrimaryKeySelective(workflowConfigDetail);
            logService.insertLog("分配权限",
                    new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(workflowConfigDetail.getActivityName()).toString(), request);
        } catch (Exception e) {
            JshException.writeFail(logger, e, systemConfigService.getLanCode());
        }
        return result;
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int deleteWorkflowConfigDetail(String id, HttpServletRequest request) throws Exception {
        return batchDeleteWorkflowConfigDetailByIds(id.toString());
    }

    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteWorkflowConfigDetail(String ids, HttpServletRequest request) throws Exception {
        return batchDeleteWorkflowConfigDetailByIds(ids);
    }


    @Transactional(value = "transactionManager", rollbackFor = Exception.class)
    public int batchDeleteWorkflowConfigDetailByIds(String ids) throws Exception {
        int result = 0;
//        String [] idArray=ids.split(",");
        //TODO 校检 流程配置表
//        List<Material> materialList=null;
//        try{
//            materialList=materialMapperEx.getMaterialListByUnitIds(idArray);
//        }catch(Exception e){
//            JshException.readFail(logger, e, systemConfigService.getLanCode());
//        }
//        if(materialList!=null&&materialList.size()>0){
//            logger.error("异常码[{}],异常提示[{}],参数,UnitIds[{}]",
//                    ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,"zh".equals(systemConfigService.getLanCode()) ? ExceptionConstants.DELETE_FORCE_CONFIRM_MSG : ExceptionConstants.DELETE_FORCE_CONFIRM_MSG_US,ids);
//            throw new BusinessRunTimeException(ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,
//                    "zh".equals(systemConfigService.getLanCode()) ? ExceptionConstants.DELETE_FORCE_CONFIRM_MSG : ExceptionConstants.DELETE_FORCE_CONFIRM_MSG_US);
//        }
//        //记录日志
//        StringBuffer sb = new StringBuffer();
//        sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
//        List<Unit> list = getUnitListByIds(ids);
//        for(Unit unit: list){
//            sb.append("[").append(unit.getName()).append("]");
//        }
//        logService.insertLog("流程设计", sb.toString(),
//                StringUtil.getRequest());
//        User userInfo=userService.getCurrentUser();
//        //校验通过执行删除操作
//        try{
//            result=unitMapperEx.batchDeleteUnitByIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
//        }catch(Exception e){
//            JshException.writeFail(logger, e, systemConfigService.getLanCode());
//        }
        return result;
    }

    public int checkIsNameExist(String id, String name) throws Exception {
        WorkflowConfigDetailExample example = new WorkflowConfigDetailExample();
        example.createCriteria().andIdNotEqualTo(id).andActivityNameEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
        List<WorkflowConfigDetail> list = null;
        try {
            list = workflowConfigDetailMapper.selectByExample(example);
        } catch (Exception e) {
            JshException.readFail(logger, e, systemConfigService.getLanCode());
        }
        return list == null ? 0 : list.size();
    }

}
